package com.crazy.web.controller.share;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.crazy.model.major.dto.UserSession;
import com.crazy.model.major.entity.TArticle;
import com.crazy.model.major.entity.TLabel;
import com.crazy.rest.RestResult;
import com.crazy.web.controller.BaseController;
import com.crazy.web.service.share.ArticleService;
import com.crazy.web.service.share.LabelService;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 文章相关接口
 * @author zhaochaofeng
 * @version 1.0
 * @date 2020/11/30 16:12
 */
@Log4j2
@Controller
@RequestMapping("/share/article")
public class ArticleController extends BaseController {

    @Autowired
    private ArticleService articleService;
    @Autowired
    private LabelService labelService;

    /**
     * 打开文章列表页
     * @return
     */
    @RequiresPermissions("article:view")
    @GetMapping("/view")
    public String view() {
        request.setAttribute("url", "/share/article/view");
        return "article/view";
    }

    /**
     * 查询文章列表
     * @param page
     * @param rows
     * @param search
     * @return
     */
    @RequiresPermissions("article:select")
    @GetMapping("/select")
    @ResponseBody
    public RestResult select(@RequestParam int page, @RequestParam int rows, @RequestParam(required = false) String search) {
        UserSession userSession = getUserSession();
        IPage<TArticle> articleIPage = articleService.find(userSession.getId(), page, rows, search);
        return RestResult.createSuccessResult("查询成功", articleIPage);
    }

    /**
     * 打开编辑页
     * @param id
     * @param model
     * @return
     */
    @RequiresPermissions("article:edit")
    @GetMapping("/edit")
    public String edit(@RequestParam(required = false) Long id, Model model) {
        UserSession userSession = getUserSession();
        TArticle tArticle = null;
        List<TLabel> labelList = null;
        if(id!=null)
        {
            tArticle = articleService.findByIdWithAuthority(userSession.getId(), id);
            labelList = labelService.findByArticleId(tArticle.getId());
        }
        model.addAttribute("article", tArticle);
        model.addAttribute("labels", labelList);
        return "article/edit";
    }

    /**
     * 保存草稿
     * @return
     */
    @RequiresPermissions("article:addDraft")
    @PostMapping("/addDraft")
    @ResponseBody
    public RestResult addDraft(@RequestParam String mdtext, @RequestParam String title, @RequestParam(required = false) Long id) {
        UserSession userSession = getUserSession();
        TArticle tArticle = new TArticle();
        tArticle.setId(id);
        tArticle.setTitleName(title);
        tArticle.setContent(mdtext);
        //不发布，代表草稿
        tArticle.setIsRelease(0);
        articleService.save(tArticle, userSession.getId());
        return RestResult.createSuccessResult("保存成功");
    }

    /**
     * 发布文章
     * @return
     */
    @RequiresPermissions("article:add")
    @PostMapping("/add")
    @ResponseBody
    public RestResult add(@RequestParam String mdtext, @RequestParam String title, @RequestParam Integer type,
                          @RequestParam Integer isPublic, @RequestParam("labelNames[]") String[] labelNames,
                          @RequestParam(required = false) Long id) {
        UserSession userSession = getUserSession();
        TArticle tArticle = new TArticle();
        tArticle.setId(id);
        tArticle.setTitleName(title);
        tArticle.setContent(mdtext);
        tArticle.setType(type);
        tArticle.setIsPublic(isPublic);
        //不发布，代表草稿
        tArticle.setIsRelease(1);
        articleService.save(tArticle, userSession.getId(), labelNames);
        return RestResult.createSuccessResult("发布成功");
    }

    /**
     * 删除文章
     * @param ids
     * @return
     */
    @RequiresPermissions("article:delete")
    @PostMapping("/delete")
    @ResponseBody
    public RestResult delete(@RequestParam("ids[]") Long[] ids) {
        articleService.delete(ids);
        return RestResult.createSuccessResult("删除成功");
    }
}
